import Vue from 'vue'

Vue.prototype.$isActive = function(route) {
  return route.path === this.$route.path
}

Vue.prototype.$toView = function(path) {
  this.$router.push(path)
}

Vue.prototype.$toLastView = function(visitedViews, view) {
  const latestView = visitedViews.slice(-1)[0]
  if (latestView) {
    this.$router.push(latestView.fullPath)
  } else {
    this.$router.push('/')
  }
}

Vue.prototype.$closeView = function(view, toPath) {
  this.$store.dispatch('tagsView/delView', view).then(({ visitedViews }) => {
    if (toPath) {
      this.$toView(toPath)
    } else {
      if (this.$isActive(view)) {
        this.$toLastView(visitedViews, view)
      }
    }
  })
}
